package com.jeesite.modules.dp.service.chatai;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.modules.dp.dao.chatai.DpChartAiUsageLogDao;
import com.jeesite.modules.dp.entity.chatai.DpChartAiUsageLogEntity;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DpChartAiUsageLogService {

    @Autowired
    private DpChartAiUsageLogDao chartAiUsageLogDao;

    public void increase() {
        String userId = UserUtils.getUser().getId();
        String date = DateUtils.formatDate(new Date(), "yyyyMMdd");
        if (chartAiUsageLogDao.count(userId, Integer.parseInt(date)) == 0) {
            DpChartAiUsageLogEntity entity = new DpChartAiUsageLogEntity();
            entity.setUserId(userId);
            entity.setChartNum(1);
            entity.setDate(Integer.parseInt(date));
            chartAiUsageLogDao.insert(entity);
        } else {
            chartAiUsageLogDao.increase(userId, Integer.parseInt(date));
        }
    }

    public int getUsageNum() {
        String userId = UserUtils.getUser().getId();
        String date = DateUtils.formatDate(new Date(), "yyyyMMdd");
        Integer usageNum = chartAiUsageLogDao.getUsageNum(userId, Integer.parseInt(date));
        return usageNum == null ? 0 : usageNum;
    }
}
